///<reference path="../typings/jquery/jquery.d.ts">
///<reference path="../typings/sharepoint/sharepoint.d.ts">
///<reference path="utilities.ts">

import m_Util = require("modules/utilities");

export module Taxonomy {

    export class Dependencies {

        public static GetTermStore(termStoreName: string): m_Util.Utilities.Dependency<SP.Taxonomy.TermStore> {
            var termStore = new m_Util.Utilities.Dependency<SP.Taxonomy.TermStore>();

            // Overwrite delegate 
            termStore.Resolve = function () {

                // Current Context
                var context: SP.ClientContext = SP.ClientContext.get_current();

                // Current Taxonomy Session
                var taxSession: SP.Taxonomy.TaxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

                // Term Stores
                var termStores: SP.Taxonomy.TermStoreCollection = taxSession.get_termStores();

                context.load(termStores);

                context.executeQueryAsync(function () {
                    var termStoreEnumerator = termStores.getEnumerator();
                    while (termStoreEnumerator.moveNext()) {
                        if (termStoreEnumerator.get_current().get_name() == termStoreName) {
                            // Update the Observable
                            termStore.AsyncObservable(termStoreEnumerator.get_current());
                            alert(termStoreEnumerator.get_current().get_name());
                        }
                    }
                },
                    function () {
                        alert("ExecuteQueryAsyncException@termStore.Resolve");
                    });
            };
            return termStore;
        }

        public static GetTermGroup(
            termStore: m_Util.Utilities.Dependency<SP.Taxonomy.TermStore>,
            termGroupName: string): m_Util.Utilities.Dependency<SP.Taxonomy.TermGroup> {
            var termGroup = new m_Util.Utilities.Dependency<SP.Taxonomy.TermGroup>();

            // Overwrite delegate 
            termGroup.Resolve = function () {

                // Current Context
                var context: SP.ClientContext = SP.ClientContext.get_current();

                // Current Taxonomy Session
                var taxSession: SP.Taxonomy.TaxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

                // Term Groups
                var termGroups: SP.Taxonomy.TermGroupCollection = termStore.AsyncObservable().get_groups();

                context.load(termGroups);

                context.executeQueryAsync(function () {
                    var termGroupsEnumerator = termGroups.getEnumerator();
                    while (termGroupsEnumerator.moveNext()) {
                        if (termGroupsEnumerator.get_current().get_name() == termGroupName) {
                            termGroup.AsyncObservable(termGroupsEnumerator.get_current());
                            alert(termGroup.AsyncObservable().get_name());
                        }
                    }
                },
                    function () {
                        alert("ExecuteQueryAsyncException@termGroup.Resolve");
                    });
            };
            return termGroup;
        }

        public static GetTermSet(
            termGroup: m_Util.Utilities.Dependency<SP.Taxonomy.TermGroup>,
            termSetName: string): m_Util.Utilities.Dependency<SP.Taxonomy.TermSet> {
            var termSet = new m_Util.Utilities.Dependency<SP.Taxonomy.TermSet>();

            // Overwrite delegate 
            termSet.Resolve = function () {

                // Current Context
                var context: SP.ClientContext = SP.ClientContext.get_current();

                // Terms for termLabel
                var termSets = termGroup.AsyncObservable().get_termSets();

                context.load(termSets);

                context.executeQueryAsync(function () {
                    var termSetEnumerator = termSets.getEnumerator();
                    while (termSetEnumerator.moveNext()) {
                        if (termSetEnumerator.get_current().get_name() == termSetName) {
                            termSet.AsyncObservable(termSetEnumerator.get_current());
                            alert(termSet.AsyncObservable().get_name());
                        }
                    }
                },
                    function () {
                        alert("ExecuteQueryAsyncException@termSet.Resolve");
                    });
            };
            return termSet;
        }

    }

}
